package com.ryhx.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectionUtil {
	static Connection conn = null;
	/**
	 * 获取数据库连接
	 * @return
	 */
	public static Connection getConnection() {
		try {
			Class.forName(ConfigUtil.driverClassName);
			String url = ConfigUtil.url;
			String username = ConfigUtil.username;
			String password = ConfigUtil.password;
			System.out.println("获取数据库连接开始-----------------》》》》" + url);
			conn = DriverManager.getConnection(url, username, password);
			System.out.println("获取数据库连接结束-----------------》》》》 " + conn);
		} catch (ClassNotFoundException e) {
			System.out.println("获取数据库连接异常-----------------》》》》");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("获取数据库连接异常-----------------》》》》");
			e.printStackTrace();
		}
		return conn;
	}
	/**
	 * 释放数据库链接
	 */
	public static void releaseConn(){
		if(conn != null){
			try {
				System.out.println("关闭数据库连接开始-----------------》》》》");
				conn.close();
				System.out.println("关闭数据库连接结束-----------------》》》》");
			} catch (SQLException e) {
				System.out.println("关闭数据库连接异常-----------------》》》》");
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		Connection connection = getConnection();
		PreparedStatement prepareStatement = connection.prepareStatement("select orderId from omorder");
		ResultSet executeQuery = prepareStatement.executeQuery();
		while (executeQuery.next()) {
			System.out.println(executeQuery.getString(1));
		}
		releaseConn();
	}
}
